package gui;

import java.awt.Component;

public class MyView {

	public static final MyView NULL = new MyView(null);
	public static final MyView DOUBLE = new MyView(null).setZ(2);

	double x, y, z;
	private double MAX_ZOOM = 10000f, MIN_ZOOM = 0.10f;
	private double UNIT_ZOOM;
	private Component c;

	public MyView(Component inC) {
		x = y = 0;
		z = 1;
		c = inC;
		evaluateUnitZoom();
	}

	public static double convert(double z) {
		return (Math.log10(z) * 50);
	}

	public static double convert(int z) {
		return Math.pow(10, z / 50f);
	}

	public double x(double inX) {
		return ((inX - x) * z);
	}

	public double y(double inY) {
		return ((inY - y) * z);
	}

	public double s(double size) {
		return (size * z);
	}

	public double xI(double inX) {
		return (inX / z + x);
	}

	public double yI(double inY) {
		return (inY / z + y);
	}

	public double sI(double size) {
		return size / z;
	}

	public void moveX(double dX) {
		x -= dX / z;
	}

	public void moveY(double dY) {
		y -= dY / z;
	}

	public MyView setZ(double newZoom) {
		if (newZoom >= MAX_ZOOM)
			newZoom = MAX_ZOOM;
		else if (newZoom <= MIN_ZOOM)
			newZoom = MIN_ZOOM;
		else {
			if (c != null) {
				x -= c.getWidth() * (1 - newZoom / z) / newZoom / 2;
				y -= c.getHeight() * (1 - newZoom / z) / newZoom / 2;
			}
			z = newZoom;
		}
		return this;
	}

	public void setX(double inX) {
		x = inX;
	}

	public void setY(double inY) {
		y = inY;
	}

	public void zoom(double dZ) {
		double newZoom = z * dZ;
		if (newZoom > MAX_ZOOM)
			newZoom = MAX_ZOOM;
		else if (newZoom < MIN_ZOOM)
			newZoom = MIN_ZOOM;
		else {
			if (c != null) {
				x += c.getWidth() * (1 - dZ) / newZoom / 2;
				y += c.getHeight() * (1 - dZ) / newZoom / 2;
			}
			z = newZoom;
		}
	}

	private void evaluateUnitZoom() {
		UNIT_ZOOM = (double) (Math.log10(MAX_ZOOM) - Math.log10(MIN_ZOOM) + 1) / 100;
	}

	public void zoomIn() {
		zoom(1 + 2 * UNIT_ZOOM);
	}

	public void zoomOut() {
		zoom(1 - UNIT_ZOOM);
	}
}